<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ResourceView extends CI_Controller {
    function _init() {
        $data = array();
        $data['mainMenu'] = makeMainMenu();
        return $data;
    }

	public function index($cat1=null,$cat2=null,$cat3=null,$cat4=null)
	{
        $data = ResourceView::_init();
        $data['categoryString']  = '';
        $data['categoriesArray'] = array();
        $data['resourcesArray']  = array();

        if (empty($cat1) && empty($cat2) && empty($cat3) && empty($cat4)) {
            $this->getFeaturedResources();
        } else {
            $lastCategory = $cat1;
            if (!empty($cat4)) { $lastCategory = $cat4; }
            else if (!empty($cat3)) { $lastCategory = $cat3; }
            else if (!empty($cat2)) { $lastCategory = $cat2; }
            
            $data['categoryString']  = $this->getCategoryTreeFromCategory($lastCategory,'string');
            $data['categoriesArray'] = $this->getCategoriesUnderCategory($lastCategory);
            $data['resourcesArray']  = $this->getResourcesByCategory($lastCategory);
        }

		$this->load->view('resource_view',$data);
	}

    public function getFeaturedResources() {

    }

    public function getCategoryTreeFromCategory($category,$render='array') {
        $categoryArray = $this->site_categories->getCategoryTreeFromCategory($category);
        if ($render === 'array') {
            return $categoryArray;
        } else if ($render === 'string') {
            $categoryString = '';
            $categoryLinkString = '';
            $a = 0;
            $categories = count($categoryArray)-1;
            foreach($categoryArray as $category) {
                $linkClass = 'arrow right';
                if ($categories == $a) {
                    $linkClass = '';
                }
                $categoryLinkString .= $category['Link'].'/';
                $categoryString .= '<a href="'.site_url('category/'.$categoryLinkString).'" class="'.$linkClass.'">'.$category['Title'].'</a>';
                $a++;
            }
            return $categoryString;
        }
    }

    public function getResourcesByCategory($category) {
        $resourceArray = $this->resources->getResourcesFromCategory($category);
        return $resourceArray;
    }

    public function getCategoriesUnderCategory($category) {
        $categoryArray = $this->site_categories->getCategoriesUnderCategory($category);
        return $categoryArray;
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */